package com.lkm.market2_0.m_imple.steelyardserver.server.sendtask;

import com.lkm.frame.P;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.CollectionHelp;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle.DevicePrinter;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsSendWriteTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.ReceiveTask;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



public class SetPrintTask extends AbsSendWriteTask {

    

    public SetPrintTask(ITaskExecutor<Object[], Void, Boolean> taskExecutor, TaskManager mTaskManager, IHardwareControler bluetoothSocket, ReceiveTask mReceiveTask,String table,String qr ) {
        super(taskExecutor, SetPrintTask.class.getName(), mTaskManager, bluetoothSocket,mReceiveTask);
        setParam(new Object[]{table,qr});
    }
    byte[] str;
    @Override
    protected byte onGetRequestCMD(int[] outDataSize) {

       String printTable= (String)getParam()[0];
       String qrString= (String)getParam()[1];

        
        int printTableLength= printTable.length();
        StringBuilder stringBuilder=new StringBuilder();
        int count=0;
        
        for (int i = 0; i < printTableLength; i++) {
            String it=printTable.substring(i,i+1);
            count+=  getCharWidth(it);
//                System.out.println(SaleFragment.getCharWidth(it)+"     "+it);
            stringBuilder.append(it);
            if(count>=32){
                count=0;
                stringBuilder.append("\n");
            }

        }
        String orderInfoStr="";


        BufferedReader br=new BufferedReader(new StringReader(stringBuilder.toString()));

        List<String> ss=new ArrayList<>();
        do {
            String s=null;
            try {
                s= br.readLine();
            }catch (Exception e){}

            if(s ==null)break;

            ss.add(s);
        }while (true);


        for(int i=ss.size()-1; i>=0;i-- ){
            orderInfoStr+=ss.get(i);
        }


        
        byte[] printContents = new DevicePrinter("gb18030").print(orderInfoStr);



//        System.out.println("====="+new Gson().toJson(str));;



//        System.out.println("====="+new Gson().toJson(str));;


        byte[] writeBytes=qrString.getBytes();

//        System.out.println("====="+new Gson().toJson(writeBytes));;




        
         str = new byte[]{0x1B,0x61,0x01,
                 0x1D,0x77,0x04,
                 0x1D,0x28,0x31,
                 0x06,0x00,
                 0x00,
                 0x00};



        int j=writeBytes.length+2;
        str[9]= (byte) (j&0x00ff);
        str[10]= (byte) ((j>>8)&0x00ff);

        str= CollectionHelp.concatByte(str,writeBytes);


        

        
        byte[] printHead=new byte[]{0x1b,0x40};

        
        printHead = CollectionHelp.concatByte(printHead, new byte[]{0x1b,0x7b,0x01});



        
        printHead = CollectionHelp.concatByte(printHead,new byte[]{0x1b,0x33,0x00});

        printContents= CollectionHelp.concatByte( printHead,printContents);

        

        str=CollectionHelp.concatByte(str,printContents);



        str=CollectionHelp.concatByte(str,new byte[]{0x1b,0x4a,0x70} );
        outDataSize[0]=str.length;
        return 0x09;
    }

    @Override
    protected byte[] onGetSendDataItem(int index) {

        int startIndex= 4096*index;
        int endIndex=Math.min(str.length,startIndex+4096);

        if(startIndex>=str.length){
            
            return null;
        }
















        return Arrays.copyOfRange(str,startIndex,endIndex);
    }

    @Override
    public void onPreExecute() {
        super.onPreExecute();
        if(P.isDebug()) {
            P.p("=======开始打印");
            if(UIP.isPrint()){
                UIP.p("Send打印：开始",mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }
    }
    @Override
    public void onCancel() {
        super.onCancel();
        if(P.isDebug()) {
            P.p("=======打印取消了");
            if(UIP.isPrint()){
                UIP.p("Send打印：取消了",mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }
    }
    @Override
    public void onFinish(Boolean aBoolean) {
        super.onFinish(aBoolean);

        if(P.isDebug()) {
            P.p("======打印是否成功："+aBoolean);
            if(UIP.isPrint()){
                UIP.p("Send打印：成功？"+aBoolean,mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }
    }
}